from rest_framework import serializers
from logsManage import models


class LogListSeriallizer(serializers.ModelSerializer):
    """
    列表序列化
    """
    user_name = serializers.CharField(source='user.name', required=False)
    grade_name = serializers.SerializerMethodField()

    def get_grade_name(self, instance):
        if instance.grade == 0:
            return '信息'
        elif instance.grade == 1:
            return '警告'
        elif instance.grade == 2:
            return '错误'
        else:
            return '信息'
    class Meta:
        model = models.LogsList
        fields = ['id', 'name', 'grade_name', 'describe', 'user_id', 'timer', 'ip', 'user_name']
